package com.softer.chengxin.traning.controller;

import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.person.domin.Person;
import com.softer.chengxin.traning.entity.TraningInfo;
import com.softer.chengxin.traning.service.TraningInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/1/19
 */
@RestController
@RequestMapping("/api/traning")
public class TraningInfoController {
    @Autowired
    private TraningInfoService traningInfoService;

    /**
     * 通过项目id查询
     *
     * @param projectId
     * @return
     */
    @PostMapping("/selectByProject")
    public List<Person> selectByProject(String projectId) {
        return traningInfoService.selectByProject(projectId);
    }

    /**
     * 是否有重复的证书编号
     *
     * @param certNo
     * @return
     */
    @PostMapping("/selectByCertNo")
    public CommonResult selectByCertNo(String certNo) {
        return traningInfoService.selectByCertNo(certNo);
    }

    /**
     * 企业端 查询列表
     *
     * @param page        分页实体
     * @param traningInfo 查询实体
     * @return 教育列表
     */
    @PostMapping("/selectAllTraning")
    public PageInfo<TraningInfo> selectAllTraning(Page page, TraningInfo traningInfo, HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new PageInfo<>();
        }
        traningInfo.setCompanyId(id.toString());
        return traningInfoService.selectAllTraning(page, traningInfo);
    }

    /**
     * 新增
     *
     * @param traningInfo
     * @return
     */
    @PostMapping("/addTraning")
    public CommonResult addTraning(TraningInfo traningInfo, HttpSession session) {
        Integer id = (Integer) session.getAttribute(Common.ID);
        traningInfo.setCompanyId(id.toString());
        return traningInfoService.addTraning(traningInfo);
    }

    /**
     * 新增
     *
     * @param id
     * @return
     */
    @PostMapping("/getInfo")
    public TraningInfo getInfo(String id) {
        return traningInfoService.getInfo(id);
    }

    /**
     * 查询待审核列表
     *
     * @param page
     * @param traningInfo
     * @return
     */
    @PostMapping("/getListByArea")
    public PageInfo<TraningInfo> getListByArea(Page page, TraningInfo traningInfo, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isEmpty(loginName)) {
            return new PageInfo<>();
        }
        return traningInfoService.getListByArea(page, traningInfo, loginName);
    }

    /**
     * 主管端 查询待审核列表
     *
     * @param page
     * @param traningInfo
     * @return
     */
    @PostMapping("/selectStateByArea")
    public PageInfo<TraningInfo> selectStateByArea(Page page, TraningInfo traningInfo, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (Common.ADMIN.equals(loginName)) {
            loginName = "";
        }
        return traningInfoService.selectStateByArea(page, traningInfo, loginName);
    }
}
